local workingnew = "$BS_fold/local2/inputs/"

clear
use "`workingnew'/ProdQ_new_klme"

tempfile params
save `params', replace

use "`workingnew'/prowess_wits_estimation_qonly_klme.dta", clear

merge m:1 nic_08_2dig using `params'
drop _merge

tabulate nic_08_2dig, sort

***Drop the single product firms that are non-comparable- i.e., missing energy and quantity data
keep if q_s!=. &  energy_intensity!=.

merge m:1 co_code1 year using "`workingnew'/first_last_years.dta"
keep if _merge==3
drop _merge

sort product_id year
xtset product_id year
bys co_code1 year: egen nprod = count(co_code1)
tsfill, full
sort product_id year
gen N = missing(L.co_code1)
gen D = missing(f.co_code1)

replace N = . if first_year == 1
replace D = . if last_year == 1

drop if missing(co_code1)

drop cleaned_number_prod
bysort co_code1 year: generate cleaned_number_prods = _N

tabulate cleaned_number_prods

gen omega_j= q_s - beta_l_hat_q * (l + ln_shares) - beta_k_hat_q* ( k + ln_shares) - beta_m_hat_q * (m + ln_shares) - beta_e_hat_q*(e + ln_shares) ///
- beta_lk_hat_q*I_sq - beta_km_hat_q*I_sq_km - beta_lm_hat_q*I_sq_lm - beta_le_hat_q*I_sq_le - beta_ke_hat_q*I_sq_ke - beta_me_hat_q*I_sq_me  

egen firm_prod = group(up12 co_code1)
egen ind_year = group(nic_08_4dig year)
egen firm_year =  group(co_code1 year)


preserve

	ivreghdfe q_s (ln_price = omega_j), absorb(up12 year) first cluster(co_code1)
	generate sample0 = e(sample)
	keep if sample0 == 1
	eststo sigma1 
	matrix FS1 = e(first)
	local F1 = round(FS1[15,1], 0.01)
	local F1: di %4.2f `F1'
	
	reghdfe ln_price omega_j, absorb(up12 year) cluster(co_code1)
	eststo FS1
	
restore

preserve

	ivreghdfe q_s (ln_price = omega_j), absorb(up12 co_code1 year) first cluster(co_code1)
	generate sample0 = e(sample)
	keep if sample0 == 1
	eststo sigma2
	matrix FS2 = e(first)
	local F2 = round(FS2[15,1], 0.01)
	local F2: di %4.2f `F2'
	
	reghdfe ln_price omega_j, absorb(up12 co_code1 year) cluster(co_code1)
	eststo FS2
	
restore

preserve

	ivreghdfe q_s (ln_price = omega_j), absorb(up12 co_code1 ind_year) first cluster(co_code1)
	generate sample0 = e(sample)
	keep if sample0 == 1
	eststo sigma3
	matrix FS3 = e(first)
	local F3 = round(FS3[15,1], 0.01)
	local F3: di %4.2f `F3'
	
	reghdfe ln_price omega_j, absorb(up12 co_code1 ind_year) cluster(co_code1)
	eststo FS3
	
restore

preserve

	ivreghdfe q_s (ln_price = omega_j), absorb(firm_prod year) first cluster(co_code1)
	generate sample0 = e(sample)
	keep if sample0 == 1
	eststo sigma4
	matrix FS4 = e(first)
	local F4 = round(FS4[15,1], 0.01)
	local F4: di %4.2f `F4'
	
	reghdfe ln_price omega_j, absorb(firm_prod year) cluster(co_code1)
	eststo FS4
	
restore

preserve

	ivreghdfe q_s (ln_price = omega_j), absorb(firm_prod ind_year) first cluster(co_code1)
	generate sample0 = e(sample)
	keep if sample0 == 1
	eststo sigma5
	matrix FS5 = e(first)
	local F5 = round(FS5[15,1], 0.01)
	local F5: di %4.2f `F5'
	
	reghdfe ln_price omega_j, absorb(firm_prod ind_year) cluster(co_code1)
	eststo FS5
	
restore

preserve

	ivreghdfe q_s (ln_price = omega_j), absorb(firm_prod firm_year) first cluster(co_code1)
	generate sample0 = e(sample)
	keep if sample0 == 1
	eststo sigma6
	matrix FS6 = e(first)
	local F6 = round(FS6[15,1], 0.01)
	local F6: di %4.2f `F6'
	
	reghdfe ln_price omega_j, absorb(firm_prod firm_year) cluster(co_code1)
	eststo FS6
	
restore

label var ln_price "$\sigma$"
label var omega_j "TFPQ"
esttab sigma1 sigma2 sigma3 sigma4 sigma5 sigma6 using "$tables/TFPQ_IV_Demand.tex" , cells(b(nostar fmt(%9.3f)) se(par fmt(%9.3f))) nodepvars ///
eqlabels(none) mlabels("IV" "IV" "IV" "IV" "IV" "IV") keep(ln_price) label ///
 collabels(none) replace  starlevels(* 0.1 ** 0.05 *** 0.01) ///
 prefoot("\hline" "First Stage F-stat & `F1' & `F2' & `F3' & `F4' & `F5' & `F6' \\" ///
 "FE 1 & Product & Product & Product &  Firm-Product & Firm-Product & Firm-Product  \\" ///
 "FE 2 & Year & Firm  & Firm & Year & SubInd-Year & Firm-Year \\" ///
 "FE 3 & - & Year  & SubInd-Year &- & - & - \\")
 
 esttab FS1 FS2 FS3 FS4 FS5 FS6 using "$tables/TFPQ_IV_Demand_FS.tex" , cells(b(nostar fmt(%9.3f)) se(par fmt(%9.3f))) nodepvars ///
eqlabels(none) mlabels("FS" "FS" "FS" "FS" "FS" "FS") keep(omega_j) label ///
 collabels(none) replace  starlevels(* 0.1 ** 0.05 *** 0.01) ///
 prefoot("\hline" "FE 1 & Product & Product & Product &  Firm-Product & Firm-Product & Firm-Product  \\" ///
 "FE 2 & Year & Firm  & Firm & Year & SubInd-Year & Firm-Year \\" ///
 "FE 3 & - & Year  & SubInd-Year &- & - & - \\")
 

